using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml.Serialization;
using System.IO;
namespace YATC
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class OptionScene : GameScene
    {
        private AudioLibrary audio;
        private float volume;
        private TimeSpan time = TimeSpan.Zero;
        private float volBar;

        private KeyboardState oldS;
        public OptionScene(Game game)
            : base(game)
        {
        	audio = (AudioLibrary) Game.Services.GetService(typeof (AudioLibrary));
        	volume = Score.volume;
        
        }
        public override void Show()
        {
            MediaPlayer.Play(audio.GetCurrentMusic());
            oldS = Keyboard.GetState();

            base.Show();
        }
        public override void Hide()
        {
            MediaPlayer.Stop();
            var ser = new XmlSerializer(typeof(float));
            var wr = new StreamWriter(@".\\Content\Setting.xml");
            ser.Serialize(wr,volume);
            wr.Close();
        	Score.volume = volume;
            base.Hide();
        }
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            time += gameTime.ElapsedGameTime;
            
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                if (time > TimeSpan.FromMilliseconds(30))
                {
                    time -= TimeSpan.FromMilliseconds(30);
                    if (volume > 0)
                        volume -= 0.01f;
                }
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                if (time > TimeSpan.FromMilliseconds(30))
                {
                    time -= TimeSpan.FromMilliseconds(30);
                    if (volume < 1)
                        volume += 0.01f;
                }
            }
            KeyboardState cur = Keyboard.GetState();
            if(cur.IsKeyDown(Keys.N) && oldS.IsKeyUp(Keys.N))
                MediaPlayer.Play(audio.GetNextMusic());

            if (cur.IsKeyDown(Keys.B) && oldS.IsKeyUp(Keys.B))
                MediaPlayer.Play(audio.GetPreviosMusic());
            oldS = cur;
            MediaPlayer.Volume = volume;
            volBar = volume*209;
            base.Update(gameTime);
        }
        public float VolBar
        {
            get{ return volBar;}
        }
    	
    }
}